package me.caofeng.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.logging.log4j.LogManager;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurationSelector;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.config
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-15 14:15
 * @Version: V1.0
 */
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"me.caofeng.dao","me.caofeng.service"},excludeFilters = @ComponentScan.Filter(
        type = FilterType.ANNOTATION,value = EnableWebMvc.class))
@MapperScan(basePackages = "me.caofeng.dao", annotationClass = Repository.class)
public class RootConfig {

    @Bean
    public BasicDataSource dataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        Properties properties = new Properties();
        InputStream inputStream = RootConfig.class.getResourceAsStream("/jdbc.properties");
        try {
            if (null != inputStream) {
                properties.load(inputStream);
                String url = (String) properties.get("url");
                String driver = (String) properties.get("driver");
                String username = (String) properties.get("username");
                String password = (String) properties.get("password");
                int initialSize = Integer.parseInt(String.valueOf(properties.get("initialSize")));
                int maxActive = Integer.parseInt(String.valueOf(properties.get("maxActive")));
                basicDataSource.setDriverClassName(driver);
                basicDataSource.setUrl(url);
                basicDataSource.setUsername(username);
                basicDataSource.setPassword(password);
                basicDataSource.setInitialSize(initialSize);
                basicDataSource.setMaxActive(maxActive);

            } else {
                LogManager.getLogger().error(" file not exists ");
            }
        } catch (IOException e) {
            LogManager.getLogger().error(e);
        }
        return basicDataSource;
    }

    @Bean()
    public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        Resource resource = new ClassPathResource("mybatis-config.xml");
        sessionFactoryBean.setConfigLocation(resource);
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect","mysql");
        pageInterceptor.setProperties(properties);
        sessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor});
        return sessionFactoryBean;
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
        return template;
    }

    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource)  ;
        manager.setRollbackOnCommitFailure(true);
        return manager;
    }
}
